﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using BrainTechLLC.Silverlight.Controls;

namespace BrainTechLLC.Silverlight.ClientApplication
{
    public partial class EditState : BaseDataVisualizer, ISingleItemDataVisualizer
    {
        public override void SetVisualizedData(DataVisualizer vis, object o) {  State = o as State; base.SetVisualizedData(vis, o); }
        public Type VisualizeType { get { return typeof(State); } }
        public string VisualizerName { get { return "EditState"; } }

        public static readonly DependencyProperty StateProperty = DependencyProperty.Register(
            "State", typeof(State), typeof(EditState), new PropertyMetadata(null, new PropertyChangedCallback(StateChanged)));

        public State State
        {
            get { return (State)GetValue(StateProperty); }
            set { SetValue(StateProperty, value); }
        }

        public static void HandleChange<T2>(DependencyObject d, DependencyPropertyChangedEventArgs e, Action<EditState, T2> action)
        {
            d.HandleChange<EditState, T2>(e, action, (t) => t.UpdateBindings());
        }

        private static void StateChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            HandleChange<State>(d, e, (t, val) => t.State = val);            
        }

        public override void UpdateBindings()
        {
            if (State != null)
            {
                if (gridData != null)
                    gridData.DataContext = State;                
            }
        }

        public override string TitleText
        {
            get
            {
                if (State == null)
                    return string.Empty;

                return string.Format("State {0} ({1} Props)", State.StateName, State.PropertiesCount);
            }
        }

        public EditState()
        {
            InitializeComponent();
        }

        private void EditCommand_Loaded(object sender, RoutedEventArgs e)
        {
            if (State != null && State.BaseStateAction == null)
            {
                editCommandToPerform.Visibility = Visibility.Collapsed;
            }
            else
            {
                // HEREHERE
                //editCommandToPerform.Window.Indent = Window.Indent + 1;
            }
        }

        private void ShowProps_Loaded(object sender, RoutedEventArgs e)
        {
            // HEREHERE
            //editStateProperties.Window.Indent = Window.Indent + 1;
        }

        public override void OnSaveData()
        {
            DataAccess.Client.StoreStateAsync(DataAccess.AssignedUniqueId, State);
        }        
    }
}